import wx
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np


class PlotFrame(wx.Frame):
    def __init__(self, parent, title):
        super(PlotFrame, self).__init__(parent, title=title, size=(800, 600))

        self.figure = Figure()

        self.axes = self.figure.add_subplot(111)

        x = np.linspace(0, 2 * np.pi, 100)
        y = np.cos(x)
        # 绘制正弦波
        self.axes.plot(x, y)
        # 设置坐标轴标签
        self.axes.set_xlabel('X axis')
        self.axes.set_ylabel('Y axis')
        # 创建一个FigureCanvasWxAgg 对象，并将其与Figure 对象关联
        self.axes.set_title('Sin Wave')
        self.canvas = FigureCanvas(self, -1, self.figure)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas, 1, wx.EXPAND)
        # 设置窗口的大小调整器
        self.SetSizer(sizer)

        self.Bind(wx.EVT_CLOSE, self.OnClose)

    def OnClose(self, event):
        self.Destroy()


if __name__ == '__main__':
    app = wx.App(False)
    frame = PlotFrame(None, 'Matplotlib Plot')
    frame.Show()
    app.MainLoop()
